var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    subAccount: [],
    hiddenNo_Data_Image: true,
    showMask: false,
    autoplay: false,
    page: 0,
    limit: 20,
    inviteInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 隐藏分享
    wx.hideShareMenu()

    this.getWaiterList();
  },

  //页面下拉刷新
  onPullDownRefresh: function() {
    this.refresh();
  },

  //刷新数据
  refresh:function(){
    var that = this;
    that.setData({
      page: 1,
    });
    setTimeout(function () {
      that.getWaiterList()
    }, 1000)
  },

  //页面上拉触底
  onReachBottom: function(e) {
    this.loadMore();
  },

  //加载更多
  loadMore: function() {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      page: this.data.page + 1
    });
    setTimeout(function() {
      that.getWaiterList()
    }, 500)
  },

  // 获取服务员列表
  getWaiterList: function() {
    var that = this;
    api.POST({
      params: {
        page: that.data.page,
        limit: that.data.limit
      },
      url: app.globalData.base_url + 'Subaccount/list',
      success: function(res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var page = paging.page;
          var resultList = res.data.data.list;
          if (page == 1) {
            var itemData = resultList;
          } else {
            var itemData = that.data.subAccount.concat(resultList);
          }
          that.setData({
            hiddenNo_Data_Image: false,
            total_page: paging.total_page,
            subAccount: itemData
          });
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function(fail) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        util.showError("网络错误");
      }
    })
  },


  /**
   * 分享
   */
  onShareAppMessage: function(res) {
    var that = this;
    var userInfo = wx.getStorageSync('accountInfo');
    var path = '/agent/pages/inviteAccount/inviteAccount?uid=' + userInfo.uid;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '邀请',
        path: path,
        imageUrl: "../../images/ic_share_pic.png"
      }
    }
  },

  //同意、不同意、撤销
  changeStatus: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var types = e.currentTarget.dataset.types;
    if (types == 1) {
      var message = '同意';
    } else if (types == 2) {
      var message = '不同意';
    } else {
      var message = '撤销';
    }
    wx.showModal({
      title: '提示',
      content: '是否确定“' + message + '”',
      cancelText: '再想想',
      confirmText: message,
      success(res) {
        if (res.confirm) {
          api.POST({
            params: {
              id: id,
              status: types
            },
            url: app.globalData.base_url + 'Subaccount/changeStatus',
            success: function(res) {
              if (util.isEmpty(res.data.code)) {
                util.showError('操作成功')
                that.refresh();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function() {
              util.showError("网络错误");
            }
          })
        }
      }
    })
  },

  //添加子帳號
  setModalStatus: function(e) {
    var that = this;
    api.POST({
      params: {},
      url: app.globalData.base_url + 'Subaccount/getQrcode',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            showMask: !that.data.showMask,
            inviteInfo: res.data.data
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function() {
        util.showError("网络错误");
      }
    })

  },


  // 阻止冒泡
  clickContent: function() {

  },
})